#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/12/11
# @Author :songtx
# @File   :write_mysql.py

import pymysql


def write_sql(time, o_i, status):  # 写入本地数据库，o_i有三个选项，do_s=513,di_s,000=err_msg
    command1 = ''
    s15, s7 = status[0:1], status[8:9]
    s14, s6 = status[1:2], status[9:10]
    s13, s5 = status[2:3], status[10:11]
    s12, s4 = status[3:4], status[11:12]
    s11, s3 = status[4:5], status[12:13]
    s10, s2 = status[5:6], status[13:14]
    s9, s1 = status[6:7], status[14:15]
    s8, s0 = status[7:8], status[15:16]
    # open_connect_mysql
    db = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cursor = db.cursor()
    if o_i == 513:
        command1 = 'INSERT INTO do_s(time,do_01,do_02,do_03,do_04,do_05,do_06,do_07,do_08,' \
                   'do_09,do_10,do_11,do_12,do_13,do_14,do_15,do_16)'
    elif o_i == 769:
        command1 = 'INSERT INTO di_s(time,di_01,di_02,di_03,di_04,di_05,di_06,di_07,di_08,' \
                   'di_09,di_10,di_11,di_12,di_13,di_14,di_15,di_16)'
    elif o_i == 000:
        command1 = 'INSERT INTO err_msg(time,err_01,err_02)'
    command2 = command1 + ' VALUE(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'

    cursor.executemany(command2, [(time, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15)])
    db.commit()
    db.close()
